﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using SCResources.DataLayer;

namespace SCResources.General
{
    /// <summary>
    /// Class to control the serie data
    /// </summary>
    [Serializable]
    public class SerieCollection
    {
        private List<Serie> activeSeries;
        private List<Serie> archivedSeries;

        public List<Serie> ActiveSeries
        {
            get
            {
                return activeSeries;
            }
            set
            {
                activeSeries = value;
            }
        }

        public List<Serie> ArchivedSeries
        {
            get
            {
                return archivedSeries;
            }
            set
            {
                archivedSeries = value;
            }
        }

        /// <summary>
        /// standaard constructor
        /// </summary>
        public SerieCollection()
        {
            this.archivedSeries = new List<Serie>();
            this.activeSeries = new List<Serie>();
        }

        /// <summary>
        /// niet standaard constructor
        /// </summary>
        /// <param name="activeSeries">lijst met active series</param>
        /// <param name="archivedSeries">lijst met archived series</param>
        public SerieCollection(List<Serie> activeSeries,List<Serie> archivedSeries)
        {
            this.activeSeries = activeSeries;
            this.archivedSeries = archivedSeries;
        }

        //all actions 
        #region Actions
        
        /// <summary>
        /// Remove Serie
        /// </summary>
        /// <param name="titel"></param>
        /// <returns></returns>
        public bool removeSerie(string titel, SelectedCollection collection)
        {
            List<Serie> tempCollection;
            if(collection.Equals(SelectedCollection.Active))
            {
                tempCollection = activeSeries;
            }
            else
            {
                tempCollection = archivedSeries;
            }

            try
            {
                tempCollection.RemoveAt(findIndex(titel, tempCollection));
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }

        /// <summary>
        /// update serie
        /// </summary>
        /// <param name="uSerie">new serie object</param>
        public bool updateSerie(Serie uSerie, string oldTitle, SelectedCollection collection)
        {
            List<Serie> tempCollection;
            if (collection.Equals(SelectedCollection.Active))
            {
                tempCollection = activeSeries;
            }
            else
            {
                tempCollection = archivedSeries;
            }

            //make sure the title can be changed
            int index;
            if(string.IsNullOrEmpty(oldTitle))
            {
                index = findIndex(uSerie.Titel, tempCollection);
            }
            else
            {
                index = findIndex(oldTitle, tempCollection);
            }
            
            //replace the old entry with the new one
            try
            {
                tempCollection.RemoveAt(index);
                tempCollection.Insert(index, uSerie);
                return true;
            }
            catch (IndexOutOfRangeException)
            {
                return false;
            }
        }

        ///// <summary>
        ///// add active serie
        ///// </summary>
        ///// <param name="titel">Titel</param>
        ///// <param name="season">Season</param>
        ///// <param name="episode">espisode</param>
        ///// <param name="nextAirDate">airdate</param>
        //public void addSerie(string titel, int season, int episode, DateTime nextAirDate, SelectedCollection collection,int frequency = 0, string coverPath = null)
        //{
        //    List<Serie> tempCollection;
        //    if (collection.Equals(SelectedCollection.Active))
        //    {
        //        tempCollection = activeSeries;
        //    }
        //    else
        //    {
        //        tempCollection = archivedSeries;
        //    }
        //    tempCollection.Add(new Serie(titel, season, episode, nextAirDate,frequency, coverPath));
        //}

        /// <summary>
        /// add active serie
        /// </summary>
        /// <param name="serie">serie object</param>
        public void addSerie(Serie serie,SelectedCollection collection)
        {
            List<Serie> tempCollection;
            if (collection.Equals(SelectedCollection.Active))
            {
                tempCollection = activeSeries;
            }
            else
            {
                tempCollection = archivedSeries;
            }
            tempCollection.Add(serie);
        }

        /// <summary>
        /// gets the series that air this day
        /// </summary> 
        /// <returns></returns>
        public List<Serie> getAiredSeries(int warningDelay)
        {
            List<Serie> airedSeries = new List<Serie>();
            if (warningDelay >= 0)
            {
                foreach (Serie serie in ActiveSeries)
                {
                    if (DateTime.Now.Date.Equals(serie.NextAirDate.Date.AddDays(warningDelay)))
                    {
                        airedSeries.Add(serie);
                    }
                }
            }

            return airedSeries;
        }

        /// <summary>
        /// update the airdate if the date is in the past. (Only active series)
        /// </summary>
        public void updateAirdates()
        {
            int warningDelay = IO_Settings.GetSettings().WarningDelay;
            for (int i = 0; i < activeSeries.Count; i++)
            {
                Serie serie = activeSeries[i];
                if(serie.NextAirDate.Date >= DateTime.Now.Date.AddDays(-60) && serie.Frequency != 0)
                {
                    while (DateTime.Now.Date > serie.NextAirDate.Date.AddDays(warningDelay))
                    {
                        serie.NextAirDate = serie.NextAirDate.Date.AddDays(serie.Frequency);
                        serie.CurEpisode++; //increase current episode
                        activeSeries[i] = serie;
                    }   
                }
            }
        }

        #endregion

        //general methodes
        #region General

        /// <summary>
        /// make active serie archived
        /// </summary>
        /// <param name="serie"></param>
        public void moveSerieActiveToArchived(Serie serie)
        {
            removeSerie(serie.Titel,SelectedCollection.Active);
            addSerie(serie,SelectedCollection.Archived);
        }

        /// <summary>
        /// make archived serie active
        /// </summary>
        /// <param name="serie"></param>
        public void moveSerieArchivedToActive(Serie serie)
        {
            removeSerie(serie.Titel,SelectedCollection.Archived);
            addSerie(serie,SelectedCollection.Active);
        }
        #endregion

        #region helpers
        /// <summary>
        /// find index of serie in serie list
        /// </summary>
        /// <param name="titel">serie titel</param>
        /// <param name="activeOrArchived">true: active , false: archived</param>
        /// <returns></returns>
        private int findIndex(string titel, List<Serie> collection)
        {
            int index = 0;
            bool found = false;


            foreach (Serie s in collection)
            {
                if (s.Titel == titel)
                {
                    found = true;
                    break;
                }
                index++;
            }

            if (found)
            {
                return index;
            }
            else
            {
                return -1;
            }
        }

        #endregion
    }
}
